/*
 * Copyright 2012 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com.
 */
package jim.h.commons;

import java.io.File;
import java.util.Collection;
import java.util.List;
import java.util.Map.Entry;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;

/**
 * @author jinggang.huangjg 2012-5-8 下午5:03:38
 */
public class Test {

    public static void main(String[] args) {
        File lib1 = new File("target/lib1");
        File lib2 = new File("target/lib2");

        List<String> list1 = Lists.newArrayList(lib1.list());
        List<String> list2 = Lists.newArrayList();

        System.out.println("精确匹配:");
        for (String fileName : lib2.list()) {
            if (list1.remove(fileName)) {
                System.out.println(fileName);
            } else {
                list2.add(fileName);
            }
        }
        System.out.println("一共" + (lib1.list().length - list1.size()) + "个");

        Multimap<String, String> map1 = ArrayListMultimap.create();
        for (String fileName : list1) {
            map1.put(fileName.replaceFirst("-\\d.*", ""), fileName);
        }

        List<String> list3 = Lists.newArrayList();
        System.out.println("\n\n模糊匹配:");
        for (String fileName : list2) {
            Collection<String> fileNames = map1.removeAll(fileName.replaceFirst("-\\d.*", ""));
            if (!fileNames.isEmpty()) {
                if (fileNames.size() > 1) {
                    System.out.println(fileName.replaceFirst("-\\d.*", "") + ":");
                }
                for (String f : fileNames) {
                    if (fileNames.size() > 1) {
                        System.out.print("\r");
                    }
                    System.out.println(fileName.replaceFirst("-\\d.*", "") + " - " + f);
                }
            } else {
                list3.add(fileName);
            }
        }

        System.out.println("\n\nlib1中未被匹配:");
        for (Entry<String, Collection<String>> fileNames : map1.asMap().entrySet()) {
            if (fileNames.getValue().size() > 1) {
                System.out.println(fileNames.getKey() + ":");
            }
            for (String fileName : fileNames.getValue()) {
                if (fileNames.getValue().size() > 1) {
                    System.out.println("\t");
                }
                System.out.println(fileName);
            }
        }

        System.out.println("\n\nlib2中未被匹配:");
        for (String fileName : list3) {
            System.out.println(fileName);
        }
    }
}
